<template>
    <div>
    {{goods}}
    <br>
    <ul>
        <li v-for="i in goods">
            {{i.name}} &ensp;&ensp;&ensp;&ensp;
            <button @click="gettotal(i.id)">商品详情</button>
        </li>
    </ul>
    <br>
    <br>
    <br>
    <br>
    {{totals}}
    <br>
    <table border="1px">
        <tr>
            <td>商品名称</td>
            <td>商品价格</td>
            <td>商品描述</td>
            <td>商品数量</td>
            <td>操作</td>
        </tr>
        <tr v-for="i in totals">
            <td>{{i.name}}</td>
            <td>{{i.price}}</td>
            <td>{{i.content}}</td>
            <td>{{i.tcount}}</td>
            <td>
                <button @click="shouchang(i.id,type)">{{mas}}</button>
            </td>
        </tr>
        <br>
        <br>
        <br>


        <ul>
            <li v-for="i in mylist">
                {{i.name}} &ensp;&ensp;&ensp;{{i['g.name']}}
            </li>
        </ul>
        
    </table>

    </div>
</template>

<script>
    export default {
        data() {
            return {
                goods:[],
                totals:[],
                userid:localStorage.getItem('userid'),
                type:1,
                mas:'收藏',
                mylist:[]
            }
        },
        methods: {
            getgoods(){
                this.$axios.get('/showgoods/').then(res=>{
                    this.goods = res.data.list
                })
            },
            //详情
            gettotal(id){
                this.$axios.get('/showgoods/?id='+id).then(res=>{
                    this.totals = res.data.list
                })
            },
            //收藏
            shouchang(id,type){
                if( !this.userid){
                    this.$router.push('klogin')
                }
                this.$axios.get('/shoucang/?id='+id+'&userid='+this.userid+'&type='+this.type).then(res=>{
                    if(res.data.code == 200){
                        this.type = res.data.type
                        this.mas = '取消收藏'
                    }else if(res.data.code == 2000){
                        this.type = res.data.type
                        this.mas = '收藏'
                    }
                })
            },
        
        },
        mounted() {
            this.getgoods()
            
        },
    }
</script>

<style scoped>

</style>